﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace ctsu.Admin.Import
{
    public static class ImportHelpers
    {
        public static SortableBindingList<ImportColumnsOutpatient> GetOutpatientImportFile(string fullFileName)
        {

            var lines = new SortableBindingList<ImportColumnsOutpatient>();

            var delimiters = new char[] { '|' };
            using (var reader = new StreamReader(fullFileName))
            {
                var cols = new ImportColumnsOutpatient();
                int count = 1;
                int iLine = 0;
                while (true)
                {
                    string line = reader.ReadLine();
                    iLine++;

                    if (line == null)
                    {
                        break;
                    }

                    if (count == 1)
                    {
                        count = 2;
                        continue;
                    }
                    else
                    {
                        try
                        {
                            cols = new ImportColumnsOutpatient();
                            cols.Line = line;
                            string[] parts = line.Split(delimiters);
                            if (parts.Length < 3)
                                continue;
                            cols.Calendar = parts[0];
                            cols.SubjectLine = parts[1];
                            cols.PatLastName = parts[2];
                            cols.PatFirstName = parts[3];
                            cols.PatMrn = parts[4];
                            cols.PatDob = parts[5];
                            cols.PatGender = parts[6];
                            cols.Protocol = parts[7];
                            cols.ScLastName = parts[8];
                            cols.ScFirstName = parts[9];
                            cols.ScEmail = parts[10];
                            cols.Pi = parts[11];
                            cols.PiContact = parts[12];
                            cols.RDoc = parts[13];
                            cols.RDocContact = parts[14];
                            cols.RDocContactBak = parts[15];
                            cols.Pl = parts[16];
                            cols.PlBak = parts[17];
                            cols.DateStart = parts[18];
                            cols.DateEnd = parts[21];
                            cols.VisitLength = parts[24];
                            cols.CcNum = parts[25];
                            cols.Location = parts[26];
                            cols.Status = parts[27];
                            cols.CostCenter = parts[28];
                            cols.Nurse = parts[30];
                            cols.Phlebotomy = parts[31];
                            cols.Processing = parts[32];
                            cols.Room = parts[33];
                            cols.EkgStudy = parts[34];
                            cols.EkgCci = parts[35];
                            cols.Vitals = parts[36];
                            cols.Ultrasound = parts[37];
                            cols.Dynamap = parts[38];
                            cols.Bp = parts[39];
                            cols.Physical = parts[40];
                            cols.Xrf = parts[41];
                            cols.Consent = parts[42];
                            cols.Inpatient = parts[43];
                            cols.Outpatient = parts[44];
                            cols.Lab = parts[45];
                            cols.Ogit = parts[46];
                            cols.Np = parts[47];
                            if (parts.Length > 146)
                                cols.OldRecBody = parts[146]; //this col does not show up all the time
                            else
                                cols.OldRecBody = "";
                        }
                        catch (Exception ex)
                        {
                            var sb = new StringBuilder();
                            sb.Append("There was an error processing this file.  The file was not in the expected format.");
                            sb.Append(Environment.NewLine);
                            sb.Append("The error occured on line " + iLine.ToString() + ".  Please correct and try again.");
                            MessageBox.Show(sb.ToString(), "File Format Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return null;
                        }
                    }
                    lines.Add(cols);
                    // Console.WriteLine("{0} field(s)", parts.Length);
                }
            }
            return lines;
        }

        public static SortableBindingList<ImportColumnsUltraSound> GetUltraSoundImportFile(string fullFileName)
        {

            SortableBindingList<ImportColumnsUltraSound> lines = new SortableBindingList<ImportColumnsUltraSound>();

            char[] delimiters = new char[] { '|' };
            using (StreamReader reader = new StreamReader(fullFileName))
            {
                var cols = new ImportColumnsUltraSound();
                int count = 1;
                var iLine = 0;
                while (true)
                {
                    string line = reader.ReadLine();
                    iLine++;

                    if (line == null)
                    {
                        break;
                    }

                    if (count == 1)
                    {
                        count = 2;
                        continue;
                    }
                    else
                    {
                        try
                        {
                            cols = new ImportColumnsUltraSound();
                            cols.Line = line;
                            string[] parts = line.Split(delimiters);
                            cols.Calendar = parts[0];
                            cols.SubjectLine = parts[1];
                            cols.PatLastName = parts[2];
                            cols.PatFirstName = parts[3];
                            cols.PatMrn = parts[4];
                            cols.PatDob = parts[5];
                            cols.PatGender = parts[6];
                            cols.Protocol = parts[7];
                            cols.ScLastName = parts[8];
                            cols.ScFirstName = parts[9];
                            cols.ScEmail = parts[10];
                            cols.Pi = parts[11];
                            cols.PiContact = parts[12];
                            cols.RDoc = parts[13];
                            cols.RDocContact = parts[14];
                            cols.RDocContactBak = parts[15];
                            cols.Pl = parts[16];
                            cols.PlBak = parts[17];
                            cols.DateStart = parts[18];
                            cols.DateEnd = parts[19];
                            cols.VisitLength = parts[20];
                            cols.CcNum = parts[21];
                            cols.Location = parts[22];
                            cols.Status = parts[23];
                            cols.CostCenter = parts[24];
                            cols.Pwv = parts[26];
                            cols.Pat = parts[27];
                            cols.Carotid = parts[28];
                            cols.Brachial = parts[29];
                            cols.Echo = parts[30];
                            cols.LowerExtremity = parts[31];
                        }
                        catch (Exception ex)
                        {
                            var sb = new StringBuilder();
                            sb.Append("There was an error processing this file.  The file was not in the expected format.");
                            sb.Append(Environment.NewLine);
                            sb.Append("The error occured on line " + iLine.ToString() + ".  Please correct and try again.");
                            MessageBox.Show(sb.ToString(), "File Format Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            
                            return null;
                        }
                    }
                    lines.Add(cols);
                    // Console.WriteLine("{0} field(s)", parts.Length);
                }
            }
            return lines;
        }

        public static SortableBindingList<ImportColumnsInpatient> GetInpatientImportFile(string fullFileName)
        {

            var lines = new SortableBindingList<ImportColumnsInpatient>();

            var delimiters = new char[] { '|' };
            using (var reader = new StreamReader(fullFileName))
            {
                var cols = new ImportColumnsInpatient();
                int count = 1;
                var iLine = 0;;
                while (true)
                {
                    string line = reader.ReadLine();
                    iLine++;

                    if (line == null)
                    {
                        break;
                    }

                    if (count == 1)
                    {
                        count = 2;
                        continue;
                    }
                    else
                    {
                        try
                        {
                            cols = new ImportColumnsInpatient();
                            cols.Line = line;
                            string[] parts = line.Split(delimiters);
                            if (parts.Length < 3)
                            {
                                if(iLine == 2)
                                {
                                    MessageBox.Show("The file delimiter must be a '|', please correct and try again.",
                                                    "File Delimiter Not Correct", MessageBoxButtons.OK,
                                                    MessageBoxIcon.Error);
                                    break;
                                }
                                else
                                {
                                    continue;
                                }
                            }
                            cols.Calendar = parts[0];
                            cols.SubjectLine = parts[1];
                            cols.PatLastName = parts[2];
                            cols.PatFirstName = parts[3];
                            cols.PatMrn = parts[4];
                            cols.PatDob = parts[5];
                            cols.PatGender = parts[6];
                            cols.Protocol = parts[7];
                            cols.ScLastName = parts[8];
                            cols.ScFirstName = parts[9];
                            cols.ScEmail = parts[10];
                            cols.Pi = parts[11];
                            cols.PiContact = parts[12];
                            cols.RDoc = parts[13];
                            cols.RDocContact = parts[14];
                            cols.RDocContactBak = parts[15];
                            cols.Pl = parts[16];
                            cols.PlBak = parts[17];
                            cols.AdmitDate = parts[18];
                            cols.DischargeDate = parts[19];
                            cols.BedDays = parts[20];
                            cols.CcNum = parts[33];
                            cols.Location = parts[34];
                            cols.Status = parts[35];
                            cols.CostCenter = parts[36];
                            cols.Nurse = parts[38];
                            cols.Phlebotomy = parts[39];
                            cols.Processing = parts[40];
                            cols.Room = parts[41];
                            cols.EkgStudy = parts[42];
                            cols.EkgCci = parts[43];
                            cols.Vitals = parts[44];
                            cols.Ultrasound = parts[45];
                            cols.Dynamap = parts[46];
                            cols.Bp = parts[47];
                            cols.Physical = parts[48];
                            cols.Xrf = parts[49];
                            cols.Consent = parts[50];
                            cols.Inpatient = parts[51];
                            cols.Outpatient = parts[52];
                            cols.Lab = parts[53];
                            cols.Ogit = parts[54];
                            cols.Np = parts[55];
                            cols.SleepWake = parts[56];
                            if (parts.Length > 57)
                                cols.Handedness = parts[57]; //this col does not show up all the time
                        }
                        catch(Exception ex)
                        {
                            var sb = new StringBuilder();
                            sb.Append("There was an error processing this file.  The file was not in the expected format.");
                            sb.Append(Environment.NewLine);
                            sb.Append("The error occured on line " + iLine.ToString() + ".  Please correct and try again.");
                            MessageBox.Show(sb.ToString(), "File Format Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return null;
                        }
                    }
                    lines.Add(cols);
                    // Console.WriteLine("{0} field(s)", parts.Length);
                }
            }
            return lines;
        }

        public static void CopyToRejectOutpatient(ImportColumnsOutpatientReject rejline, ImportColumnsOutpatient line)
        {
            rejline.Bp = line.Bp;
            rejline.Calendar = line.Calendar;
            rejline.CcNum = line.CcNum;
            rejline.Consent = line.Consent;
            rejline.CostCenter = line.CostCenter;
            rejline.DateEnd = line.DateEnd;
            rejline.DateStart = line.DateStart;
            rejline.Dynamap = line.Dynamap;
            rejline.EkgCci = line.EkgCci;
            rejline.EkgStudy = line.EkgStudy;
            rejline.Inpatient = line.Inpatient;
            rejline.Lab = line.Lab;
            rejline.Location = line.Location;
            rejline.Np = line.Np;
            rejline.Nurse = line.Nurse;
            rejline.Ogit = line.Ogit;
            rejline.OldRecBody = line.OldRecBody;
            rejline.Outpatient = line.Outpatient;
            rejline.PatDob = line.PatDob;
            rejline.PatFirstName = line.PatFirstName;
            rejline.PatGender = line.PatGender;
            rejline.PatLastName = line.PatLastName;
            rejline.PatMrn = line.PatMrn;
            rejline.Phlebotomy = line.Phlebotomy;
            rejline.Physical = line.Physical;
            rejline.Pi = line.Pi;
            rejline.PiContact = line.PiContact;
            rejline.Pl = line.Pl;
            rejline.PlBak = line.PlBak;
            rejline.Processing = line.Processing;
            rejline.Protocol = line.Protocol;
            rejline.RDoc = line.RDoc;
            rejline.RDocContact = line.RDocContact;
            rejline.RDocContactBak = line.RDocContactBak;
            rejline.Room = line.Room;
            rejline.ScEmail = line.ScEmail;
            rejline.ScFirstName = line.ScFirstName;
            rejline.ScLastName = line.ScLastName;
            rejline.Status = line.Status;
            rejline.SubjectLine = line.SubjectLine;
            rejline.Ultrasound = line.Ultrasound;
            rejline.VisitLength = line.VisitLength;
            rejline.Vitals = line.Vitals;
            rejline.Xrf = line.Xrf;

        }

        public static void CopyToRejectInpatient(ImportColumnsInpatientReject rejline, ImportColumnsInpatient line)
        {
            rejline.Bp = line.Bp;
            rejline.Calendar = line.Calendar;
            rejline.CcNum = line.CcNum;
            rejline.Consent = line.Consent;
            rejline.CostCenter = line.CostCenter;
            rejline.AdmitDate = line.AdmitDate;
            rejline.DischargeDate = line.DischargeDate;
            rejline.Dynamap = line.Dynamap;
            rejline.EkgCci = line.EkgCci;
            rejline.EkgStudy = line.EkgStudy;
            rejline.Inpatient = line.Inpatient;
            rejline.Lab = line.Lab;
            rejline.Location = line.Location;
            rejline.Np = line.Np;
            rejline.Nurse = line.Nurse;
            rejline.Ogit = line.Ogit;            
            rejline.Outpatient = line.Outpatient;
            rejline.PatDob = line.PatDob;
            rejline.PatFirstName = line.PatFirstName;
            rejline.PatGender = line.PatGender;
            rejline.PatLastName = line.PatLastName;
            rejline.PatMrn = line.PatMrn;
            rejline.Phlebotomy = line.Phlebotomy;
            rejline.Physical = line.Physical;
            rejline.Pi = line.Pi;
            rejline.PiContact = line.PiContact;
            rejline.Pl = line.Pl;
            rejline.PlBak = line.PlBak;
            rejline.Processing = line.Processing;
            rejline.Protocol = line.Protocol;
            rejline.RDoc = line.RDoc;
            rejline.RDocContact = line.RDocContact;
            rejline.RDocContactBak = line.RDocContactBak;
            rejline.Room = line.Room;
            rejline.ScEmail = line.ScEmail;
            rejline.ScFirstName = line.ScFirstName;
            rejline.ScLastName = line.ScLastName;
            rejline.Status = line.Status;
            rejline.SubjectLine = line.SubjectLine;
            rejline.Ultrasound = line.Ultrasound;
            rejline.BedDays = line.BedDays;
            rejline.Vitals = line.Vitals;
            rejline.Xrf = line.Xrf;
            rejline.SleepWake = line.SleepWake;
            rejline.Handedness = line.Handedness;

        }

        public static void CopyToRejectUltraSound(ImportColumnsUltraSoundReject rejline, ImportColumnsUltraSound line)
        {            
            rejline.Calendar = line.Calendar;
            rejline.CcNum = line.CcNum;
            rejline.CostCenter = line.CostCenter;
            rejline.DateEnd = line.DateEnd;
            rejline.DateStart = line.DateStart;
            rejline.Location = line.Location;
            rejline.PatDob = line.PatDob;
            rejline.PatFirstName = line.PatFirstName;
            rejline.PatGender = line.PatGender;
            rejline.PatLastName = line.PatLastName;
            rejline.PatMrn = line.PatMrn;
            rejline.Pi = line.Pi;
            rejline.PiContact = line.PiContact;
            rejline.Pl = line.Pl;
            rejline.PlBak = line.PlBak;
            rejline.Protocol = line.Protocol;
            rejline.RDoc = line.RDoc;
            rejline.RDocContact = line.RDocContact;
            rejline.RDocContactBak = line.RDocContactBak;
            rejline.ScEmail = line.ScEmail;
            rejline.ScFirstName = line.ScFirstName;
            rejline.ScLastName = line.ScLastName;
            rejline.Status = line.Status;
            rejline.SubjectLine = line.SubjectLine;
            rejline.VisitLength = line.VisitLength;
            rejline.Pwv = line.Pwv;
            rejline.Pat = line.Pat;
            rejline.Carotid = line.Carotid;
            rejline.Brachial = line.Brachial;
            rejline.Echo = line.Echo;
            rejline.LowerExtremity = line.LowerExtremity;
        }

        public static string ValidateMrn(ImportColumnsOutpatient line)
        {
            Boolean bIsValid = true;
            string lmrn = line.PatMrn.Trim();
            lmrn = lmrn.Replace("-", "");
            lmrn = lmrn.Replace(" ", "");

            int iMrn = 0;

            if (!int.TryParse(lmrn, out iMrn))
                bIsValid = false;

            if (iMrn == 0)
                bIsValid = false;

            //if length is < 8 - padd with leading zeros
            if (bIsValid)
            {
                if (lmrn.Trim().Length < 8)
                {
                    var add = 8 - lmrn.Length;
                    for (int i = 0; i < add; i++)
                        lmrn = "0" + lmrn;
                }
            }

            if (bIsValid)
            {
                if (lmrn.Trim().Length > 8)
                    bIsValid = false;
            }

            if (!bIsValid)
                return line.PatLastName + line.PatFirstName + DateTime.Parse(line.PatDob).ToString("MMddyyyy");

            return lmrn;

        }

        internal static string ValidateMrn(ImportColumnsInpatient line)
        {
            Boolean bIsValid = true;
            string lmrn = line.PatMrn.Trim();
            lmrn = lmrn.Replace("-", "");
            lmrn = lmrn.Replace(" ", "");

            int iMrn = 0;

            if (!int.TryParse(lmrn, out iMrn))
                bIsValid = false;

            if (iMrn == 0)
                bIsValid = false;

            //if length is < 8 - padd with leading zeros
            if (bIsValid)
            {
                if (lmrn.Trim().Length < 8)
                {
                    var add = 8 - lmrn.Length;
                    for (int i = 0; i < add; i++)
                        lmrn = "0" + lmrn;
                }
            }

            if (bIsValid)
            {
                if (lmrn.Trim().Length > 8)
                    bIsValid = false;
            }

            if (!bIsValid)
                return line.PatLastName + line.PatFirstName + DateTime.Parse(line.PatDob).ToString("MMddyyyy");

            return lmrn;
        }

        internal static string ValidateMrn(ImportColumnsUltraSound line)
        {
            Boolean bIsValid = true;
            string lmrn = line.PatMrn.Trim();
            lmrn = lmrn.Replace("-", "");
            lmrn = lmrn.Replace(" ", "");

            int iMrn = 0;

            if (!int.TryParse(lmrn, out iMrn))
                bIsValid = false;

            if (iMrn == 0)
                bIsValid = false;

            //if length is < 8 - padd with leading zeros
            if (bIsValid)
            {
                if (lmrn.Trim().Length < 8)
                {
                    var add = 8 - lmrn.Length;
                    for (int i = 0; i < add; i++)
                        lmrn = "0" + lmrn;
                }
            }

            if (bIsValid)
            {
                if (lmrn.Trim().Length > 8)
                    bIsValid = false;
            }

            if (!bIsValid)
                return line.PatLastName + line.PatFirstName + DateTime.Parse(line.PatDob).ToString("MMddyyyy");

            return lmrn;
        }

        internal static int GetRoomId (string room)
        {
            if (room.Contains("TBA"))
                return 20;

            switch (room)
            {
                case "ROOM 1":
                    return 2;
                case "ROOM 2":
                    return 3;
                case "ROOM 3":
                    return 4;
                case "ROOM 4":
                    return 5;
                case "ROOM 5":
                    return 2;
                case "ROOM 6":
                    return 7;
                case "ROOM 15":
                    return 8;
                case "ROOM 16":
                    return 9;
                case "ROOM 17":
                    return 10;
                case "ROOM 18":
                    return 11;
                case "ROOM 19":
                    return 12;
                case "ROOM 20":
                    return 13;
                case "LAB PROCESSING":
                    return 14;
                case "SUITE 1":
                    return 15;
                case "SUITE 2":
                    return 16;
                case "SUITE 3":
                    return 17;
                case "SUITE 4":
                    return 18;
                case "SUITE 5":
                    return 19;
                case "TBA":
                    return 20;
            }
            return 0;
        }
    }
}
